package com.datamation.swadeshi.view;

import java.util.ArrayList;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.ListViewDataAdapter;
import com.datamation.swadeshi.adapter.MergeAdapter;
import com.datamation.swadeshi.adapter.SetTitleAdapter;
import com.datamation.swadeshi.model.ContentItem;
//import com.datamation.swadeshi.view.FragmentSalesManagement.ChartDataAdapter;
import com.github.mikephil.charting.data.BarData;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@SuppressLint("NewApi")
public class FragmentInventoryControl extends Fragment{
	
	public FragmentInventoryControl() {}

	ArrayList<ContentItem> InvenObjects;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		getActivity().getActionBar().removeAllTabs();
		getActivity().getActionBar().setNavigationMode(0);
		setHasOptionsMenu(true);
		
        View rootView = inflater.inflate(R.layout.inventory_control, container, false);
        //set title
		SetTitleAdapter objTitle =new SetTitleAdapter();
		objTitle.createCutomActionBarTitle("Inventory Control", getActivity());
		
		TypedArray inventoryicons = getResources().obtainTypedArray(R.array.listView_icons_for_inventory);
		
		InvenObjects = new ArrayList<ContentItem>(); 
		MergeAdapter mergeAdapter = new MergeAdapter();
		
//		ArrayList<BarData> list = new ArrayList<BarData>();
//        list.add(generateData(1));
//        ChartDataAdapter chartAdapter = new ChartDataAdapter(getActivity().getApplicationContext(), list);
        
		InvenObjects.add(new ContentItem("Loading", "",inventoryicons.getResourceId(0, -1)));
		InvenObjects.add(new ContentItem("Unloading", "",inventoryicons.getResourceId(1, -1)));
		InvenObjects.add(new ContentItem("Adjustment Addition", "",inventoryicons.getResourceId(2, -1)));
		InvenObjects.add(new ContentItem("Adjustment Deduction", "",inventoryicons.getResourceId(3, -1)));
		InvenObjects.add(new ContentItem("Damage", "",inventoryicons.getResourceId(4, -1)));
		
		ListViewDataAdapter adapter = new ListViewDataAdapter(getActivity().getApplicationContext(), InvenObjects);
		
		ListView lv = (ListView) rootView.findViewById(R.id.listView1);
	        
	    mergeAdapter.addAdapter(adapter);
	    
	    lv.setAdapter(mergeAdapter);
	    
	    lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					
					FragmentTransaction transaction = getFragmentManager().beginTransaction();
					transaction.replace(R.id.frame_container, new FragmentInventoryControlLoading());
					transaction.addToBackStack(null);
					transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
					transaction.commit();
					
					Toast.makeText(getActivity(), InvenObjects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					
					break;
				case 1:
					
					FragmentTransaction transactionUL = getFragmentManager().beginTransaction();
					transactionUL.replace(R.id.frame_container, new FragmentInventoryControlUnLoading());
					transactionUL.addToBackStack(null);
					transactionUL.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
					transactionUL.commit();
					
					Toast.makeText(getActivity(), InvenObjects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 2:
					FragmentTransaction transactionAA = getFragmentManager().beginTransaction();
					transactionAA.replace(R.id.frame_container, new FragmentInventoryControlAdjustment());
					transactionAA.addToBackStack(null);
					transactionAA.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
					transactionAA.commit();
					Toast.makeText(getActivity(), InvenObjects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 3:
					Toast.makeText(getActivity(), InvenObjects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 4:
					Toast.makeText(getActivity(), InvenObjects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					FragmentTransaction transactionDamge = getFragmentManager().beginTransaction();
					transactionDamge.replace(R.id.frame_container, new FragmentInventoryControlDamage());
					transactionDamge.addToBackStack(null);
					transactionDamge.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
					transactionDamge.commit();
					Toast.makeText(getActivity(), InvenObjects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;

				}
			}
	    	
		});
	    
	    rootView.setFocusableInTouchMode(true);
		rootView.requestFocus();
		
		rootView.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				Log.i("", "keyCode: " + keyCode);
	            if( keyCode == KeyEvent.KEYCODE_BACK ) {
	            	Toast.makeText(getActivity(),"Back Navigationid is disable", Toast.LENGTH_SHORT).show();
	               // getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	                return true;
	            }else if ((keyCode == KeyEvent.KEYCODE_HOME)) {
	    			
	    			getActivity().finish();
	    			
	    			return true;

	    		} else {
	                return false;
	            }
			}
		});
		
		
		return rootView;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		for (int i = 0; i < menu.size(); ++i) {
			menu.removeItem(menu.getItem(i).getItemId());
		}
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	
	

}
